package com.rightsidetech.mall.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rightsidetech.mall.dto.request.BrandPageReq;
import com.rightsidetech.mall.dto.request.BrandReq;
import com.rightsidetech.mall.dto.request.BrandUpdateReq;
import com.rightsidetech.mall.dto.response.BrandListResp;
import com.rightsidetech.mall.dto.response.BrandPageResp;
import com.rightsidetech.mall.entity.MallBrand;
import com.rightsidetech.mall.mapper.MallBaseInfoMapper;
import com.rightsidetech.mall.mapper.MallBrandMapper;
import com.rightsidetech.mall.service.MallBrandService;
import com.rightsidetech.commom.base.DelReq;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.Date;
import java.util.List;

@Service("mallBrandService")
public class MallBrandServiceImpl implements MallBrandService {

    @Resource
    private MallBrandMapper mallBrandMapper;
    @Resource
    private MallBaseInfoMapper mallBaseInfoMapper;

    @Override
    public ResponseData save(BrandReq req, String createName,Long createId) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long count = mallBrandMapper.selectCountByName(req.getBrandName());
        if(count > 0){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"商品品牌已经存在");
        }
        MallBrand brand = new MallBrand();
        brand.setCreatedAt(new Date());
        brand.setBrandName(req.getBrandName());
        brand.setIsDeleted((byte)0);
        brand.setCreaterName(createName);
        brand.setCreaterId(createId);
        if (mallBrandMapper.insertSelective(brand) <= 0) {
            return ResponseData.fail("添加失败");
        }
        return ResponseData.success(CodeIdEnum.Success);
    }

    @Override
    public ResponseData updateBrand(BrandUpdateReq req,String updaterName,Long updaterId) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        MallBrand brand = mallBrandMapper.selectByPrimaryKey(req.getId());
        if (ObjectUtil.isEmpty(brand) || brand.getIsDeleted() == 1) {
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"更新的数据不存在");
        }
        if(!brand.getBrandName().equals(req.getBrandName())){
            Long count = mallBrandMapper.selectCountByName(req.getBrandName());
            if(count > 0){
                return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"商品品牌已经存在");
            }
        }
        brand = new MallBrand();
        brand.setId(req.getId());
        brand.setBrandName(req.getBrandName());
        brand.setUpdatedAt(new Date());
        brand.setUpdaterName(updaterName);
        brand.setUpdaterId(updaterId);
        if (mallBrandMapper.updateByPrimaryKeySelective(brand) <= 0) {
            return ResponseData.fail("更新失败");
        }
        return ResponseData.success(CodeIdEnum.Success);
    }

    @Override
    public ResponseData<PageInfo<BrandPageResp>> pageBrand(BrandPageReq req) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        PageHelper.startPage(req.getPageNo(),req.getPageSize());
        List<BrandPageResp> result = mallBrandMapper.selectPageList(req);
        return ResponseData.success(new PageInfo<>(result));
    }

    @Override
    public ResponseData isDelete(DelReq req) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        MallBrand brand = mallBrandMapper.selectByPrimaryKey(req.getId());
        if(ObjectUtil.isEmpty(brand) || brand.getIsDeleted() == 1){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"删除的数据不存在");
        }
        Long count = mallBaseInfoMapper.selectCountByBrandId(req.getId());
        if(count > 0){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"已有基础商品使用该品牌,不允许删除");
        }
        brand = new MallBrand();
        brand.setId(req.getId());
        brand.setIsDeleted((byte)1);
        brand.setUpdatedAt(new Date());
        if (mallBrandMapper.updateByPrimaryKeySelective(brand) <= 0) {
            return ResponseData.fail("删除失败");
        }
        return ResponseData.success(CodeIdEnum.Success);
    }

    @Override
    public ResponseData<List<BrandListResp>> selectList(String brandName) {
        List<BrandListResp> result = mallBrandMapper.selectList(brandName);
        return ResponseData.success(result);
    }
}
